package com.atguigu.common.result;
/*统一结果返回类
 * */

import lombok.Data;

@Data
public class Result<T> {
    private Integer code;//状态码  200:successful  400 : file
    private String message;//返回信息
    private T data;//数据

    //私有化
    private Result() {

    }
    //    封装返回数据
    protected static <T> Result<T> build(T data) {
        Result<T> result = new Result<T>();
        if (data != null)
            result.setData(data);
        return result;
    }

    public static <T> Result<T> build(T body, Integer code, String message) {
        Result<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> build(T body,ResultCodeEnum resultCodeEnum){
        Result<T> result = new Result<>();
//        封装数据

        if (body != null) {
            result.setData(body);
        }
        //        状态码
        result.setCode(resultCodeEnum.getCode());
//        返回信息  成功|失败
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }
    //    成功  无数据
    public static <T> Result<T> success(T data) {
        return  build(data, ResultCodeEnum.SUCCESS);
    }
    //    成功  有数据
    public static <T> Result<T> success() {
        return build(null, ResultCodeEnum.SUCCESS);
    }
    //    失败
    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(T data) {
        return  build(data, ResultCodeEnum.FAIL);
    }

    public Result<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public Result<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
